/**
 * 懒汉式单例
 */
public class SingletonDemo02 {
    //私有构造方法
    private SingletonDemo02() {
    }

    //定义一个对象,类初始化时，不立即加载该对象
    private static SingletonDemo02 instance;

    //获取实例，增加synchronized，效率较低！
    public static synchronized SingletonDemo02 getInstance() {
        if (instance == null) {
            instance = new SingletonDemo02();
        }
        return instance;

    }

}

class SingleTonTest02 {
    public static void main(String[] args) {
        SingletonDemo02 instance01 = SingletonDemo02.getInstance();
        SingletonDemo02 instance02 = SingletonDemo02.getInstance();
        System.out.println(instance01 == instance02);  //返回true,代表同一个对象
    }
}
